package dk.ucn.datamatiker.jmaweather;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GlobalWeatherFragment extends Fragment
{

	private View _view;
	private boolean _updating = false;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{

		_view = inflater.inflate(R.layout.fragment_global_weather, container, false);
		
		Button btnLookupCityWeather = (Button)_view.findViewById(R.id.btnLookupCityWeather);
		
		btnLookupCityWeather.setOnClickListener(new View.OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				updateWeather();
			}
			
		});
		
		return _view;

	}
	
	private void updateWeather()
	{
		
		EditText txtCityName = (EditText)_view.findViewById(R.id.txtCityName);
		final String cityName = txtCityName.getText().toString();
		
		if (!_updating)
		{
			if (cityName.length() > 0)
			{
				
				_updating = true;
				
				Toast.makeText(getActivity().getApplicationContext(), getString(R.string.updating_text), Toast.LENGTH_SHORT).show();
				
				Thread updateThread = new Thread(new Runnable()
				{
					
					@Override
					public void run()
					{
						
						final WeatherData weatherData = WeatherData.fromCity(cityName);
						
						getActivity().runOnUiThread(new Runnable()
						{
							
							@Override
							public void run()
							{
								
								Toast.makeText(getActivity().getApplicationContext(), getString(R.string.updated_text), Toast.LENGTH_SHORT).show();
								
								if (weatherData != null)
								{
									
									TextView txtGlobalWeatherLocation = (TextView)_view.findViewById(R.id.txtGlobalWeatherLocation);
									txtGlobalWeatherLocation.setText(weatherData.getLocation().getName());
									
									TextView txtGlobalWeatherDescription = (TextView)_view.findViewById(R.id.txtGlobalWeatherDescription);
									txtGlobalWeatherDescription.setText(weatherData.getDescription());
									
									TextView txtGlobalWeatherTemperature = (TextView)_view.findViewById(R.id.txtGlobalWeatherTemperature);
									txtGlobalWeatherTemperature.setText(Double.toString(weatherData.getTemperature()));
									
									TextView txtGlobalWeatherTemperatureUnit = (TextView)_view.findViewById(R.id.txtGlobalWeatherTemperatureUnit);
									txtGlobalWeatherTemperatureUnit.setText(weatherData.getTemperatureUnit());
									
									ImageView imgGlobalWeather = (ImageView)_view.findViewById(R.id.imgGlobalWeather);
									imgGlobalWeather.setImageBitmap(weatherData.getIcon());	
									
								}
								
								_updating = false;
								
							}
							
						});
						
					}
					
				});
				updateThread.start();
				
			}
		}
		
	}

}
